﻿/****************************************************************
  Author:  Ievgenii Nazaruk, ievgenii.nazaruk@gmail.com 
 License:  Microsoft Public License (Ms-PL)
          ( http://www.opensource.org/licenses/ms-pl.html or
            http://www.microsoft.com/opensource/licenses.mspx )
*****************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Frontend
{
    public class InvalidTimeException : Exception
    {
        public TimeSpan SpecifiedTime = new TimeSpan();

        public InvalidTimeException(TimeSpan time)
        {
            this.SpecifiedTime = time;
        }
    }

    public class TimeSlot
    {
        public static readonly TimeSpan MinTime = new TimeSpan(0,  0, 0);
        public static readonly TimeSpan MaxTime = new TimeSpan(24, 0, 0);
        
        private TimeSpan m_begin = new TimeSpan(MinTime.Ticks);
        public TimeSpan Begin
        {
            get
            {
                return m_begin;
            }

            set
            {
                if (value < MinTime || value > MaxTime)
                {
                    throw new InvalidTimeException(value);
                }

                m_begin = value;
            }
        }

        private TimeSpan m_end = new TimeSpan(MaxTime.Ticks);
        public TimeSpan End
        {
            get
            {
                return m_end;
            }

            set
            {
                if (value < MinTime || value > MaxTime)
                {
                    throw new InvalidTimeException(value);
                }

                m_end = value;
            }
        }

        public TimeSpan Duration
        {
            get
            {
                return End - Begin;
            }
        }

        public TimeSlot()
        {
        }

        public TimeSlot(TimeSpan begin, TimeSpan end)
        {
            Begin = begin;
            End = end;

            if (End < Begin)
            {
                throw new ArgumentException("End time is less then Begin time."); 
            }
        }

        public override string ToString()
        {
            string strFormat = "{0,2:00}:{1,2:00} - {2,00:00}:{3,00:00}";

            return String.Format(strFormat,Begin.Hours, Begin.Minutes,
                                           End.Hours, End.Minutes);
        }

        public override bool Equals(object obj)
        {
            if (Object.ReferenceEquals(this, obj))
            {
                return true;
            }

            TimeSlot other = obj as TimeSlot;
            if (other == null)
            {
                return false;
            }

            return Begin == other.Begin && End == other.End;
        }

        public override int GetHashCode()
        {
            return Begin.GetHashCode() + 87 * End.GetHashCode();
        }
    }
}
